package flex.cc.order.manage.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;

import flex.cc.PublicUtil;
import flex.cc.medical.dto.entity.ImportCardEntity;
import flex.cc.order.manage.dto.vo.OrderMainVo;
import flex.cc.order.manage.enums.OrderServiceEnum;
import flex.cc.order.manage.enums.OrderStatusEnum;
import flex.cc.order.manage.service.DownLoadService;
import flex.cc.order.manage.service.OrderMainService;
import flex.cc.util.ExportUtil;

/**
*
* @author WANGCONG
* 2021年9月18日下午12:36:56
* @desc 服务单导出
*/
@Service("downLoadService")
public class DownLoadServiceImpl implements DownLoadService {

	@Resource
    private OrderMainService orderMainService;
	
	/**
	* @author WANGCONG
	* 2021年9月18日下午12:43:32
	* @desc 服务单导出
	* @param flag: 1部分导出；2全部导出
	*/
	@SuppressWarnings("deprecation")
	@Override
	public void downLoadSelection(HttpServletRequest request, HttpServletResponse response, 
			OrderMainVo orderMain, String fileParentPath, String fileName, String flag) {
		// 表名
        String title = fileName;
        // 表头数据
        String[] headers = {"服务单号","来源单号","渠道来源","服务单状态","服务类型","联系人","联系地址","救援商","创建日期","创建时间","图片张数","省","市","区"};
        String[] fields = {"orderNo","ownerOrderNo","sourceName","status","serviceItem","userName","address","serverName","createD","startTime","imgCount",
    			"province","city","district"};
        
        IPage<OrderMainVo> page = null;
        if("1".equals(flag)){
        	page = orderMainService.queryOrderListWithDataIds(orderMain);
        }else{
        	page = orderMainService.exportOrder(orderMain);
        }
        List<OrderMainVo> records = page.getRecords();
    	for(int i=0; i<records.size(); i++){
    		OrderMainVo orderMainVo = records.get(i);
    		if(PublicUtil.isNotEmpty(orderMainVo.getStatus())){
    			orderMainVo.setStatus(OrderStatusEnum.getName(orderMainVo.getStatus()));
    		}
    		if(PublicUtil.isNotEmpty(orderMainVo.getServiceItem())){
    			orderMainVo.setServiceItem(OrderServiceEnum.getName(orderMainVo.getServiceItem()));
    		}
//    		orderMainVo.setSourceName( "00".equals(orderMainVo.getSourceName())? "自营" : "阳光" );
    		String sourceName = orderMainVo.getSourceName();
    		if("00".equals(sourceName)){
    			sourceName = "自营";
    		}else if("01".equals(sourceName)){
    			sourceName = "阳光";
    		}else if("02".equals(sourceName)){
    			sourceName = "中华";
    		}else{
    			
    		}
    		orderMainVo.setSourceName(sourceName);
    		orderMainVo.setUserName(PublicUtil.isNotEmpty(orderMainVo.getUserName()) ? orderMainVo.getUserName() : "");
    		orderMainVo.setAddress(PublicUtil.isNotEmpty(orderMainVo.getAddress()) ? orderMainVo.getAddress() : "");
    		orderMainVo.setServerName(PublicUtil.isNotEmpty(orderMainVo.getServerName()) ? orderMainVo.getServerName() : "");
    		orderMainVo.setProvince(PublicUtil.isNotEmpty(orderMainVo.getProvince()) ? orderMainVo.getProvince() : "");
    		orderMainVo.setCity(PublicUtil.isNotEmpty(orderMainVo.getCity()) ? orderMainVo.getCity() : "");
    		orderMainVo.setDistrict(PublicUtil.isNotEmpty(orderMainVo.getDistrict()) ? orderMainVo.getDistrict() : "");
    	}
        if (!PublicUtil.isEmpty(records)) {
            try {
                SXSSFWorkbook wb = ExportUtil.createWorkbook(10, false);//创建工作簿对象
                ExportUtil.exportExcel(title, headers, fields, 0, wb, records);
                File fileParent = new File(fileParentPath);
                if (!fileParent.exists()) { // 若目录不存在则创建
                    fileParent.mkdirs();
                }

                File file = new File(fileParentPath + File.separator + fileName + ".xlsx");
                if (file.exists()) { // 若文件已存在则删除
                    file.delete();
                }
                try {
                    wb.write(new FileOutputStream(file));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
	}

	/**
	* @author WANGCONG
	* 2022年2月9日下午17:01:35
	* @desc 医护导入卡券，错误数据导出
	*/
	@Override
	public void downLoadMedicalCard(HttpServletRequest request, HttpServletResponse response,
			List<ImportCardEntity> records, String fileParentPath, String fileName) {
		// 表名
        String title = fileName;
        // 表头数据
        String[] headers = {"类型编码","类型名称","服务商名称","卡券序列码","激活兑换码","备注"};
        String[] fields = {"activityId","activityName","serviceName","cardNum","activateCode","remark"};
        if (!PublicUtil.isEmpty(records)) {
            try {
                SXSSFWorkbook wb = ExportUtil.createWorkbook(10, false);//创建工作簿对象
                ExportUtil.exportExcel(title, headers, fields, 0, wb, records);
                File fileParent = new File(fileParentPath);
                if (!fileParent.exists()) { // 若目录不存在则创建
                    fileParent.mkdirs();
                }
                File file = new File(fileParentPath + File.separator + fileName + ".xlsx");
                if (file.exists()) { // 若文件已存在则删除
                    file.delete();
                }
                try {
                    wb.write(new FileOutputStream(file));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
	}

}
